package com.kakaxi.mybilibili.module.user.presenter;

import android.content.Context;
import android.graphics.Bitmap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kakaxi.mybilibili.bean.MemberBean;
import com.kakaxi.mybilibili.common.WebParamConst;
import com.kakaxi.mybilibili.module.user.model.ILoginModel;
import com.kakaxi.mybilibili.module.user.model.LoginModel;
import com.kakaxi.mybilibili.module.user.view.ILoginView;
import com.vincestyling.netroid.Listener;
import com.vincestyling.netroid.NetroidError;

import java.util.Map;

/**
 * 登录控制层实现类
 * Created by kakaxi on 16/12/4.
 */

public class LoginPresenter implements ILoginPresenter {

    private ILoginView mView;

    private ILoginModel mModel;

    public LoginPresenter(ILoginView loginView) {
        mView = loginView;
        mModel = new LoginModel();
    }

    @Override
    public void initLogin(Context context) {
        mView.showInitLoading();
        mModel.initLogin(context, new Listener<String>() {
            @Override
            public void onSuccess(String response) {
                super.onSuccess(response);
                mView.hideInitLoading();
                mView.initComplete(true);
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                mView.hideInitLoading();
                mView.initComplete(false);
            }
        });
    }

    @Override
    public void getCaptchaImage() {
        mModel.getCaptchaImage(new Listener<Bitmap>() {
            @Override
            public void onSuccess(Bitmap response) {
                super.onSuccess(response);
                mView.setCaptchaImage(response);
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                mView.onCaptchaFailed();
            }
        });
    }

    @Override
    public void doLogin(String username, String password, String captchaStr) {
        mView.showLoading();
        mModel.doLogin(username, password, captchaStr, new Listener<Map<String, String>>() {
            @Override
            public void onSuccess(Map<String, String> cookieResponse) {
                super.onSuccess(cookieResponse);
                String userId = null;
                if (cookieResponse != null && cookieResponse.containsKey(ILoginModel
                        .COOKIE_NAME_USERID)) {
                    userId = cookieResponse.get(ILoginModel.COOKIE_NAME_USERID);
                    getUserInfo(userId);
                } else {
                    mView.hideLoading();
                    mView.showMessage("登录失败,请重试!");
                }
                mView.saveLoginCookie(userId, cookieResponse);
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                mView.hideLoading();
                mView.showMessage(error.getMessage());
            }
        });
    }

    /**
     * 获取用户信息
     *
     * @param userId 用户id
     */
    private void getUserInfo(String userId) {
        mModel.getUserInfo(userId, new Listener<JSONObject>() {
            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
                mView.hideLoading();
                MemberBean memberBean = null;
                if (response.getBoolean("status")) {
                    memberBean = JSON.parseObject(response.getJSONObject(WebParamConst
                            .RESPONSE_DATA).toString(), MemberBean.class);
                }
                mView.saveUserInfo(memberBean);
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                mView.hideLoading();
                mView.saveUserInfo(null);
            }
        });
    }

}
